package com.yft.service.login;

import com.yft.dao.base.BaseConstant;
import com.yft.dao.login.SysMemberDao;
import com.yft.entity.base.Order;
import com.yft.entity.base.Pager;
import com.yft.entity.login.SysMember;
import com.yft.util.MD5Encrypt;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service("sysMemberService")
public class SysMemberServiceImp implements SysMemberService {
	Logger log = Logger.getLogger(SysMemberServiceImp.class);
	
	@Autowired(required = true)
	private SysMemberDao sysMemberDao;
	
	public Object addSysMember(SysMember sysMember) {
		return sysMemberDao.addSysMember(sysMember);
	}

	public boolean checkPassword(String loginMail, String password) {
		return false;
	}

	public SysMember currentMember(HttpServletRequest request) {
		return (SysMember)request.getSession().getAttribute(BaseConstant.SESSION_MEMBER);
	}

	public SysMember getSysMember(Integer memberPK) {
		return sysMemberDao.getSysMember(memberPK);
	}

	public List<SysMember> getSysMemberList(SysMember condition, Order order,Pager pager) {
		return sysMemberDao.getSysMemberList(condition, order, pager);
	}

	public boolean isExistLoginId(String loginID) {
		return false;
	}
	
	@Transactional
	public SysMember sysMemberLogin(String loginID, String password, String loginIP) {
		if(loginID==null) return null;
		loginID = loginID.trim();
		if(loginID.length()==0) return null;
		SysMember sysMember = getSysMember(loginID,password);
		if(sysMember==null) return null;
		//sysMember.setLastLoginTime(sysMember.getNowLoginTime());
		SysMember updateSysMember = new SysMember();
		updateSysMember.setLastLoginTime(sysMember.getNowLoginTime());
		//sysMember.setNowLoginTime(new Date());
		updateSysMember.setSysMemberId(sysMember.getSysMemberId());
		updateSysMember.setNowLoginTime(new Date());
		updateSysMember.setLastLoginIp(loginIP);
		updateSysMember.setLoginSum((sysMember.getLoginSum()==null?0:sysMember.getLoginSum())+1);
		sysMemberDao.updateValues(updateSysMember,null);
		return sysMember;
	}

	@Transactional
	public boolean updateValues(SysMember condition,String cumstomStr){
		return sysMemberDao.updateValues(condition,cumstomStr);
	}
	
	private SysMember getSysMember(String loginID,String password){

		loginID = StringUtils.trimToNull(loginID);
		password = StringUtils.trimToNull(password);
		if (loginID == null || password == null) return null;
		try{
			password = MD5Encrypt.EncodeAsHex(StringUtils.trimToNull(password));
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			return null;
		}
		SysMember sysMember = sysMemberDao.getSysMemberByLoginId(loginID);
		if (sysMember==null) return null;
		if (!sysMember.getPassword().equals(password)) return null;
		return sysMember;
	}
}
